/*
 * By: Werner Schuster (murphee)
 * 
 * Copyright (c) 2005-2006 Werner Schuster (murphee)
 * 
 * This file is part of the EclipseShell project. EclipseShell is subject 
 * to the Eclipse Public License (EPL) available at
 * http://www.eclipse.org/org/documents/epl-v10.php
 * or in the file /epl-v10.html in the source for other info
 * see the /eclipseshell.license file
 */

package net.sf.eclipseshell;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The main plugin class to be used in the desktop.
 */
public class EclipseShellPlugin extends AbstractUIPlugin {

	public static final String ECLIPSE_SHELL_PLUGIN_ID = "net.sf.eclipseshell.EclipseShellPlugin";
	//The shared instance.
	private static EclipseShellPlugin plugin;
	private ResourceBundle resourceBundle_; 
	
	/**
	 * The constructor.
	 */
	public EclipseShellPlugin() {
		plugin = this;
		try {
			resourceBundle_ = ResourceBundle.getBundle("net.sf.eclipseshell.EclipseShellProperties");
		} catch (MissingResourceException x) {
			resourceBundle_ = null;
		}
	}

	/**
	 * This method is called upon plug-in activation
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
	}

	/**
	 * This method is called when the plug-in is stopped
	 */
	public void stop(BundleContext context) throws Exception {
		super.stop(context);
		plugin = null;
	}

	/**
	 * Returns the shared instance.
	 */
	public static EclipseShellPlugin getDefault() {
		return plugin;
	}

	public ResourceBundle getResourceBundle(){
		return resourceBundle_;
	}
	
	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path.
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return AbstractUIPlugin.imageDescriptorFromPlugin("BeanshellPlugin", path);
	}

	public void log(int severity, String message, Throwable ex) {
		ILog log = getDefault().getLog();
		// this is needed because Status needs a non-null message
		if(message == null){
			if(ex != null){
				message = ex.toString();
			} else {
				message = "[No message available]";
			}
		}
		log.log(new Status(severity, ECLIPSE_SHELL_PLUGIN_ID, IStatus.OK, message, ex));
	}
}
